#

| Chapter 31 Creating Custom Commands in vi |

## 31.16 vi Macro for Splitting Long Lines

When you add text to the start of a line and make the line longer than your screen's width, vi won't break ("wrap") the line unless your cursor crosses the wrapmargin point near the right-hand edge of the screen. You can get lines that are too long.

exrc | Here are two macros that cut (Kut) the current line:

>     map K 78^V|lBhr^M
>     map K 078lF r^M

The first macro doesn't seem to work on some versions of vi. It's the better one though, because it uses the `|` (vertical bar) command to move to column 78, even if there are TABs in the line. Then it moves one more character to the right (if it can), moves back to the start of the word, moves back one more character onto the blank or tab before the word, and replaces that character with a RETURN.

The second macro counts TABs as single characters, but it works on every version of vi I've tried. It moves to the left edge, then to the 79th character, then back to the previous space. Finally, it replaces that space with a carriage return.

You might try adding a `J` to the start of either macro. That'll join the next line to the current one before cutting; it might make a nicer "wrap." Another way to do this is with a filter-through (30.22) and the fmt (30.37) command:

>     !!fmt

That will break the current line at the 72nd column or before, though it also might change the spacing after periods (`.`) or replace leading TABs with spaces.

- JP

31.15 Fooling vi into Allowing Complex Macros |  | 32. GNU Emacs